package com.saul.server.business.pc28.task;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.saul.server.business.pc28.Pc28Forecast;
import com.saul.server.business.pc28.entity.ActualResult;
import com.saul.server.business.pc28.entity.Canada28;
import com.saul.server.business.pc28.entity.Canada28100;
import com.saul.server.business.pc28.entity.Canada2880;
import com.saul.server.business.pc28.mapper.ActualResultMapper;
import com.saul.server.business.pc28.mapper.Canada28100Mapper;
import com.saul.server.business.pc28.mapper.Canada2880Mapper;
import com.saul.server.business.pc28.service.Canada28100Service;
import com.saul.server.business.pc28.service.Canada2880Service;
import com.saul.server.business.pc28.service.Canada28DataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 枫伦
 * @DESCRIPTION
 * @create 2021/6/13 4:24 下午
 */
@Component
@Slf4j
public class ForcecastTask {
    @Autowired
    Canada28DataService canada28DataService;
    @Autowired
    Canada2880Mapper canada2880Mapper;
    @Autowired
    Canada28100Mapper canada28100Mapper;
    @Autowired
    Canada2880Service canada2880Service;
    @Autowired
    Canada28100Service canada28100Service;
    @Autowired
    ActualResultMapper actualResultMapper;

    /***
     * @DESCRIPTION: 此方法为定时更新预测的结果
     * @author 枫伦
     * @params: []
     * @return: void
     * @Date: 2021/6/14 5:54 下午 
     * @Modified By:  
     */
    @Scheduled(cron = "0 0/3 * * * ?")
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void addForcastData() {
//        log.info("预测数据开始" + LocalDateTime.now());
        ActualResult actuakDataNewest = null;
        //先获取期数
        try {
            actuakDataNewest = canada28DataService.getActuakDataNewest();
            QueryWrapper issueNumberWrapper = new QueryWrapper();
            issueNumberWrapper.eq("issue_number", actuakDataNewest.getIssueNumber());
            List<Canada2880> canada2880List = canada2880Mapper.selectList(issueNumberWrapper);
            List<Canada28100> canada28100List = canada28100Mapper.selectList(issueNumberWrapper);
            issueNumberWrapper.eq("is_open", 0);
            ActualResult actualResult = actualResultMapper.selectOne(issueNumberWrapper);
            if (CollectionUtil.isNotEmpty(canada2880List) && CollectionUtil.isNotEmpty(canada28100List) && ObjectUtil.isNotNull(actualResult)) {
                //有数据的情况下不需要新增,直接return
                return;
            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (actuakDataNewest == null) {
            return;
        }
        Map<String, Object> map80 = Pc28Forecast.getForecastResult(80, 0);
        Canada28 type1 = (Canada28) map80.get("type1");
        Canada28 type2 = (Canada28) map80.get("type2");
        Canada28 type3 = (Canada28) map80.get("type3");
        ArrayList list80 = new ArrayList<Canada2880>();
        list80.add(type1.setIssueNumber(actuakDataNewest.getIssueNumber() + 1).setAlgorithmType(1));
        list80.add(type2.setIssueNumber(actuakDataNewest.getIssueNumber() + 1).setAlgorithmType(2));
        list80.add(type3.setIssueNumber(actuakDataNewest.getIssueNumber() + 1).setAlgorithmType(3));
        canada2880Service.saveBatch(list80);
        Map<String, Object> map100 = Pc28Forecast.getForecastResult(100, 0);
        Canada28 type1_100 = (Canada28) map100.get("type1");
        Canada28 type2_100 = (Canada28) map100.get("type2");
        Canada28 type3_100 = (Canada28) map100.get("type3");
        ArrayList list100 = new ArrayList<Canada28100>();
        list100.add(type1_100.setIssueNumber(actuakDataNewest.getIssueNumber() + 1).setAlgorithmType(1));
        list100.add(type2_100.setIssueNumber(actuakDataNewest.getIssueNumber() + 1).setAlgorithmType(2));
        list100.add(type3_100.setIssueNumber(actuakDataNewest.getIssueNumber() + 1).setAlgorithmType(3));
        canada28100Service.saveBatch(list100);
//        ActualResult initdata = new ActualResult();
//        initdata.setIssueNumber(actuakDataNewest.getIssueNumber() + 1);
//        initdata.setIsOpen(1);
//        initdata.setEnableStatus(1);
//        actualResultMapper.insert(initdata);
    }
}
